package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.util.DateUtils;
import org.footballmanager.pandafootball.system.domain.FootSj;
import org.footballmanager.pandafootball.system.domain.JsonReturn;
import org.footballmanager.pandafootball.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private FootSjService footSjService;
    @Autowired
    private FootMissionService footMissionService;
    @Autowired
    private FootMissionHistoryService footMissionHistoryService;
    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootCupbmService footCupbmService;
    @Autowired
    private FootClubService footClubService;
    @Autowired
    private FootRankService footRankService;
    @Autowired
    private FootTtService footTtService;
    @Autowired
    private PersonService personService;
    @Autowired
    private FootPersonService footPersonService;
    @Autowired
    private FootRewardService footRewardService;

    @RequestMapping(value = "/processPage", method = RequestMethod.GET)
    @ResponseBody
    public JsonReturn processPage() {
        String baseUrl = "http://pesdb.net/pes2018";
        personService.getPage(baseUrl, 2, 306);
        return new JsonReturn(Constants.OK);
    }

    @RequestMapping(value = "/processTotalPerson", method = RequestMethod.GET)
    @ResponseBody
    public JsonReturn processTotalPerson() throws IOException {
        personService.readTotalfile("d://football/result.xls");
        return new JsonReturn(Constants.OK);
    }

    @RequestMapping(value = "/processImageUrl", method = RequestMethod.GET)
    @ResponseBody
    public JsonReturn processImageUrl() {
        String baseUrl = "http://pesdb.cn/Images/players/player_";
        String ext = ".png";
        personService.processImageUrl(baseUrl, ext);
        return new JsonReturn(Constants.OK);
    }

    @RequestMapping(value = "/resetDj", method = RequestMethod.GET)
    @ResponseBody
    public JsonReturn resetDj() throws IOException {
        footPersonService.resetDj();
        return new JsonReturn(Constants.OK);
    }

    @RequestMapping(value = "/search")
    public String search() {
        return "foot/task/List";
    }

    @RequestMapping(value = "/processTask")
    @ResponseBody
    public JsonReturn processMissionEveryDay(HttpServletRequest request, @RequestParam Integer type) {
        FootSj sj = footSjService.getRunningSj();
        Date date = new Date();
        switch (type) {
            case 1:
                footMissionService.addNewMission(sj.getSjzt(), request);
                break;
            case 2:
                footMissionHistoryService.processHistoryRecord(request);
                break;
            case 3:
                footSjService.processFirstDay(sj);
                break;
            case 4:
                footSjService.process21Day(sj);
                break;
            case 5:
                //结束八分之一决赛,开始四分之一决赛
                footCupbmService.endCupGames(2, request);
                break;
            case 6:
                //结束四分之一决赛,开始半决赛
                footCupbmService.endCupGames(3, request);
                break;
            case 7:
                //结束半决赛,开始决赛
                footCupbmService.endCupGames(4, request);
                break;
            case 8:
                int day = DateUtils.getNowDay(date);
                int endDay = DateUtils.getMonthEnd(date);
                footSjService.process28Day(date, day, endDay);
                break;
            case 9:
                footSjService.processLastDay(date);
                break;
            case 10:
                footUserService.processUserNoLogin();
                break;
            case 11:
                footClubService.processFixBySj(sj.getId());
                break;
            case 12:
                footRankService.processFixBySj(sj.getId());
                break;
            case 13:
                footTtService.processFixBySj(sj.getId());
                break;
            case 14:
                footRewardService.clearRewardFree();
                break;
            default:
                return new JsonReturn(Constants.ERROR);
        }
        return new JsonReturn(Constants.OK);
    }
}
